let db = require('./01-封装函数db')
const { md5 } = require('utility');
const express = require('express')
var jwt = require('jsonwebtoken');
const app = express()
app.use(express.urlencoded({ extended: true }));
app.post('/api/login', function (req, res) {
    let { username, password } = req.body
    password = md5(password)
    // console.log(`select * from user where username="${username}" and password="${password}"`);
    db.db(`select * from user where username="${username}" and password="${password}"`, (err, result) => {
        // console.log(result);
        if (err) throw err
        if (result.length > 0) {
            let token = jwt.sign({ id: result[0].id }, 'shhhhh', { expiresIn: '2h' });
            // console.log(result[0].id);
            res.send({ status: 0, message: '登录成功', token: 'bearer ' + token })
        } else {
            res.send({ status: 1, message: '账号或密码错误' })
        }
    })
})

app.listen(3000, () => {
    console.log('服务器启动');
})